
var method_name_L =
['UIPlaySound$$get_canPlay', 'UIPlaySound$$OnDisable', 'UIPlaySound$$OnHover', 'UnityEngine.Component$$GetComponents<UIPlaySound>', 'UIPlaySound$$OnSelect', 'UIPlaySound$$Start', 'UIPlaySound$$OnClick', 'UIPlaySound$$.ctor', 'UIPlaySound$$OnEnable', 'UIPlaySound$$OnPress', 'UIPlaySound$$Play']

var address_L=[9751012, 9751508, 9751524, 10863156, 9751808, 9751244, 9751756, 9751980, 9751256, 9751640, 9751272]

var libso_name = "libil2cpp.so"

// main
var main_entry = hook_many


function hook_many(){
    var module = Process.findModuleByName(libso_name)


    var offsets = address_L;


    offsets.forEach(function(offset, i){
        try {
            each_attach(module, offset, i)
        } catch (error) {
            console.log("error offset --- "+ offset + " ignored ... ");
        }
        
    })
}


function each_attach(module, offset, i){
    if (offset == "0x") 
        return
    var addr = module.base.add( parseInt(offset) )
    Interceptor.attach(addr, {
        onEnter: function(args){

           // if(method_name_L.indexOf('Update')===-1)
            if(method_name_L[i].indexOf('Update')==-1)
            console.log('hit addr:  ' + addr +  "--" +  method_name_L[i] + ' : ' + address_L[i])

            if(method_name_L[i].indexOf('OnEnable')>-1)
            printNativeStack(this.context ,method_name_L[i])

        }, onLeave: function(retval){

            return retval
        }
    })
}


function hook_one(){

    var module = Process.findModuleByName(libso_name)

    var targ = 0x0
    module.base.add( targ )
    Interceptor.attach(addr, {
        onEnter: function(args){
            console.log( JSON.stringify(this.context) );

        }, onLeave: function(retval){

            return retval
        }
    })

}

var is_hook = false
function hook_dlopen(){
    var dlopen = Module.findExportByName(null, 'dlopen')
    Interceptor.attach(dlopen, {
        onEnter:function(args){
            this.call_hook = false;
            var so_name = ptr(args[0]).readCString()
            if (so_name && so_name.indexOf(libso_name) >= -1){
            // if (so_name && so_name.indexOf('Managed/UnityEngine.CoreModule.dll.so') >= 0){
                console.log('dlopen: ', ptr(args[0]).readCString())
                // printNativeStack(this.context, 'dlopen' )


                // this.call_hook = true
            }
        },onLeave:function(retval){
            if (this.call_hook && is_hook==false){
                console.log('find', libso_name);
                // console.log('find'+ 'Managed/UnityEngine.CoreModule.dll.so');
                is_hook= true
                hook_native()
            }
            return retval;
        }
    })


}


function printNativeStack(context, name) {
    var trace = Thread.backtrace(context, Backtracer.FUZZY).map(DebugSymbol.fromAddress).join('\r\n');
    LogPrint('=============================' + name + ' Native Stack start=======================');
    LogPrint(trace);
    LogPrint('=============================' + name + ' Native Stack end=======================\r\n');
}


function LogPrint(log) {
    var theDate = new Date();
    var hour = theDate.getHours();
    var minute = theDate.getMinutes();
    var second = theDate.getSeconds();
    var mSecond = theDate.getMilliseconds();


    hour < 10 ? hour = '0' + hour : hour;
    minute < 10 ? minute = '0' + minute : minute;
    second < 10 ? second = '0' + second : second;
    mSecond < 10 ? mSecond = '00' + mSecond : mSecond < 100 ? mSecond = '0' + mSecond : mSecond;
    var time = hour + ':' + minute + ':' + second + ':' + mSecond;
    var threadid = Process.getCurrentThreadId();
    console.log('[' + time + ']' + '->threadid:' + threadid + '--' + log);


}


function main(){
    main_entry()
}


setImmediate(main)
